<?php

namespace app\common\model;

use think\Model;

/**
 * 货品管理
 */
class Products Extends Model
{

    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';
    // 追加属性
    protected $append = [
    ];

    public function getImageAttr($vaule){
        return getRealUrl($vaule);
    }

    public function goods(){
        return $this->belongsTo('Goods','goods_id','id');
    }

    /**
     * 根据货品ID获取货品信息
     * @param $id
     * @return array
     */
    public function getProductInfo($id)
    {
        $product = $this->with(['goods'=>function($query){
            $query->field('id,name,bn,spes_desc');
        }])->field('*')->where(['id' => $id])->find();

        if (!$product) {
            $result = [
                'status' => false,
                'msg'    => 'Get failed',
                'data'   => $product
            ];
            return $result;
        }

        $goods = $product['goods'];

        //如果是多规格商品，算多规格
        if ($goods['spes_desc']) {
            $defaultSpec = [];
            $spesDesc    = unserialize($goods['spes_desc']);

            //设置on的效果
            $productSpesDesc = getProductSpesDesc($product['spes_desc']);
            foreach ((array)$spesDesc as $k => $v) {
                foreach ((array)$v as $j) {
                    $defaultSpec[$k][$j]['name'] = $j;
                    if ($productSpesDesc[$k] == $j) {
                        $defaultSpec[$k][$j]['is_default'] = true;
                    } else {
                        $defaultSpec[$k][$j]['product_id'] = 0;
                    }
                }
            }

            //取出刨除去当前id的其他兄弟货品
            $map = [
                'goods_id' => ['eq', $product['goods_id']],
                'id' => ['neq', $id],
            ];
            $product_list = $this->where($map)->select();

            if ($product_list) {
                $product_list = collection($product_list)->toArray();
            } else {
                $product_list = [];
            }
            foreach ($product_list as $k => $v) {
                $product_list[$k]['temp_spes_desc'] = getProductSpesDesc($v['spes_desc']);
            }
            //遍历二维多规格信息，设置货品id
            foreach ($defaultSpec as $k => $j) {
                foreach ($j as $v => $l) {
                    //如果是默认选中的，不需要找货品信息
                    if (isset($l['is_default'])) {
                        continue;
                    }
                    $tempProductSpesDesc     = $productSpesDesc;
                    $tempProductSpesDesc[$k] = $v;
                    //循环所有货品，找到对应的多规格
                    foreach ($product_list as $a) {
                        if (!array_diff_assoc($a['temp_spes_desc'], $tempProductSpesDesc)) {
                            $defaultSpec[$k][$v]['product_id'] = $a['id'];
                            break;           //找到了，就不循环剩下的货品了，没意义
                        }
                    }
                }
            }
            $product['default_spes_desc'] = $defaultSpec;
        }

        $product = $product->toArray();

        $result = [
            'status' => true,
            'msg'    => 'Get success',
            'data'   => $product
        ];
        return $result;
    }
}
